﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using XbsWebAPI.Application;
using Microsoft.Extensions.Logging;

namespace XbsWebAPI.MainHost.Controllers
{
    //[Route("api/[controller]")]
    //[ApiController]
    [ApiController]
    [Route("api/xbs_v1/[controller]")]
    public class GuestController : ControllerBase
    {
        private readonly IGuestAppService _guestAppService;
        public readonly ILogger<GuestController> _logger;

        public GuestController(IGuestAppService guestAppService, ILogger<GuestController> logger) {
            _guestAppService = guestAppService;
            _logger = logger;
        }


        ///<summary>
        ///新建房客
        /// </summary>
        /// <param name="model"></param>
        [HttpPost,Route("[action]")]
        public async Task<ResultDto<string>> AddGuest(CreateGuestDto model) {
            _logger.LogInformation("========AddGuest========");
            //房客手机号码正则表达式
            Regex reg = new Regex(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
            if (!reg.IsMatch(model.PhoneNumber))
            {
                _logger.LogError("手机号格式错误");
                return new ResultDto<string>()
                {
                    Code = -1,
                    Message = "操作失败",
                    Result = "手机号格式错误"
                };
                
            }
            var result = await _guestAppService.CreateGuestAsync(model);
            return result;
        }


        ///<summary>
        ///编辑房客信息
        ///</summary>
        [HttpPost, Route("[action]")]
        public async Task<ResultDto<string>> EditGuest(EditGuestDto model) {
            var result= await _guestAppService.UpdateGuestAsync(model);
            return result;
        }

        ///<summary>
        ///删除房客
        /// </summary>
        [HttpPost, Route("[action]")]
        public async Task<ResultDto<string>> DeleteGuest(Guid id) {
            var result = await _guestAppService.RemoveGuestAsync(id);
            return result;
        }

        /// <summary>
        /// 根据Id获取房客信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<GetGuestDtoNew> FindGuestById(Guid id) {
            var result = _guestAppService.GetGuestById(id);
            return result;
        }

        /// <summary>
        /// 分页查询房客
        /// </summary>
        /// <param name="data">模糊查询条件</param>
        /// <param name="pageNumber">第几页，默认1</param>
        /// <param name="pageSize">每页多少条，默认10</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<PageListDto<ListGuestDto>> FindGuestPagedList(string data, int? pageNumber, int? pageSize)
        {
            return _guestAppService.GetGuestPagedList(new Application.QueryModel.QueryGuest() { Data = data,PageNumber = pageNumber,PageSize = pageSize}) ;
        }
    }
}
